package com.cat.dataStructure;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-using-strategy/description/
 * @create 2025/9/8 22:25
 * @since JDK17
 */

public class Solution62 {
    public long maxProfit(int[] prices, int[] strategy, int k) {
        int n = prices.length;
        long[] p1 = new long[n + 1], p2 = new long[n + 1];
        for (int i = 0; i < n; i++) {   // p1原策略能挣多少
            p1[i + 1] = p1[i] + prices[i] * strategy[i];
            p2[i + 1] = p2[i] + prices[i];  //
        }
        long ans = p1[n];   // 不修改
        for (int i = k; i <= n; i++) {  // 修改一次
            ans = Math.max(ans, p1[i - k] + p1[n] - p1[i - k / 2 + 1] - (p2[i - k / 2] - p2[i - k]) + p2[i] - p2[i - k / 2]);
        }

        return ans;
    }
}
